﻿<div class="layui-card">
	<div class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding-top:0px;padding-bottom:0px;" lay-filter="SearchForm">

	</div>
	<div class="layui-card-body">
		<table id="tblList" lay-filter="tblList"></table>
		<input type="hidden" id="hidSortData" value="" />
		<script type="text/html" id="toolbarHead">
			<div class="layui-form-item" style="margin: auto;">
				<div class="layui-inline">
					<button class="layui-btn layuiadmin-btn-list" lay-event="sort">保存排序</button>
					<span style="height: 30px; color:blue;">拖拽数据行可进行排序</span>
				</div>
			</div>
		</script>
		<script type="text/html" id="tblStatusCol">
			<input type="checkbox" name="checkOut" value="{{d.FieldName}}" lay-skin="switch" lay-text="已禁用|已启用" lay-filter="CheckOutStatus" {{ (d.Disabled) ? "checked" : "" }}>
		</script>
		<script type="text/html" id="tblRequiredCol">
			<input type="checkbox" name="checkOutRequired" value="{{d.FieldName}}" lay-skin="switch" lay-text="必填|可空" lay-filter="CheckOutRequired" {{ (d.EnableNull) ? "checked" : "" }}>
		</script>
		<script type="text/html" id="tblSearchCol">
			<input type="checkbox" name="checkOutSearch" value="{{d.FieldName}}" lay-skin="switch" lay-text="是|否" lay-filter="CheckOutSearch" {{ (d.EnableShowOnSearchForm) ? "checked" : "" }}>
		</script>
		<script type="text/html" id="tblBarCol">
			{{# if(d.FieldLevel != "0"){ }}
			<button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</button>
			{{# } }}
		</script>
	</div>
</div>
@Html.AntiForgeryToken()
@section scripts {
	<script type="text/javascript">
		layui.use(['form', 'table', 'soulTable'], function () {
			var table = layui.table;
			var form = layui.form;
			var soulTable = layui.soulTable;
			var ModelType = "@ViewBag.ModelType";
			var TemplateID = "@ViewBag.TemplateID";
			var SearchParam = { "ModelType": ModelType, "TemplateID": TemplateID };

			var myCols = [
				{ type: 'numbers', title: '序号', width: 40, fixed: 'left' },
				{ field: 'FieldName', width: 200, title: '字段名', align: 'center' },
				{ field: 'FieldAlias', width: 200, title: '字段别名', align: 'center' },
				{ field: 'FieldTypeText', width: 200, title: '字段类型', align: 'center' },
				{ field: 'FieldLevel', width: 100, title: '字段级别', align: 'center', templet: function (d) { return (d.FieldLevel == 0) ? "<a class='layui-btn layui-btn-xs'>系统</a>" : "自定义"; } },
				{ field: 'Description', minWidth: 200, title: '字段描述', align: 'left' },
				{ field: 'Disabled', width: 100, title: '状态', align: 'center', templet: '#tblStatusCol' },
				{ field: 'EnableNull', width: 100, title: '必填', align: 'center', templet: '#tblRequiredCol' },
				{ field: 'EnableShowOnSearchForm', width: 100, title: '允许搜索', align: 'center', templet: '#tblSearchCol' },
				{ width: 100, title: '操作', align: "left", templet: '#tblBarCol' }
			];

			//加载表格
			table.render({
				elem: '#tblList'
				, url: '/Admin/CommonModel/GetTemplateFieldList'
				, toolbar: '#toolbarHead'
				, where: SearchParam
				, request: {
					pageName: 'PageNum' //页码的参数名称，默认：page
					, limitName: 'PageSize' //每页数据量的参数名，默认：limit
				}
				, cols: [myCols]
				, parseData: function (res) { //res 即为原始返回的数据
					return {
						"code": 0, //解析接口状态
						"msg": "", //解析提示文本
						"count": res.RecordTotal, //解析数据长度
						"data": res.RowList //解析数据列表
					};
				}
				, limits: [10, 15, 20, 25, 50, 100]
				, limit: 100000
				, page: false
				, rowDrag: {//拖动排序
					done: function (obj) {
						// 完成时（松开时）触发，如果拖动前和拖动后无变化，则不会触发此方法
						var myData = "";
						for (i = 0; i < obj.cache.length; i++) {
							myData += obj.cache[i].FieldName + ",";
						}
						$("#hidSortData").val(myData);
					}
				}
				, done: function () {
					soulTable.render(this);// 拖拽排序
				}
			});
			//监听表格头部工具栏
			table.on('toolbar(tblList)', function (obj) {
				if (obj.event === 'sort') {
					var myData = $("#hidSortData").val();
					if (myData == null || myData == "") {
						layer.msg("排序数据没有变化");
						return false;
					}
					var param = { "ModelType": ModelType, "TemplateID": TemplateID, "datas": myData };
					$.ajax({
						type: "POST",
						url: "/Admin/CommonModel/SetTemplateFieldOrderSort",
						data: param,
						headers: {
							"X-CSRF-TOKEN-JXWebHost": $("input[name='AntiforgeryFieldname']").val()
						},
						error: function (data, status, e) {
							layer.msg('排序失败!');
						},
						success: function (data) {
							if (data.Result == "ok") {
								$("#hidSortData").val("");
								layer.msg('排序成功!');
								setTimeout('window.location.reload()', 1000);
							}
							else {
								layer.msg(data.Result);
							}
						}
					});
					return false;
				}
			});
			//监听表格操作列
			table.on('tool(tblList)', function (obj) {
				if (obj.event === 'del') {
					var param = { "ModelType": ModelType, "TemplateID": TemplateID, "FieldName": obj.data.FieldName };
					DeleteByParam("/Admin/CommonModel/DelTemplateField",param);
					return false;
				}
			});
			//监听禁用开关
			form.on('switch(CheckOutStatus)', function (data) {
				var url = '/Admin/CommonModel/UpdateTemplateFieldStatus';
				var Disabled = false;
				if (this.checked) { //设为禁用状态
					Disabled = true;
				} else { //设为启用状态
					Disabled = false;
				}
				var param = { "ModelType": ModelType, "TemplateID": TemplateID, "FieldName": data.value, "Disabled": Disabled };
				UpdateSwitch(data, url, param);
			});
			//监听必填开关
			form.on('switch(CheckOutRequired)', function (data) {
				var url = '/Admin/CommonModel/UpdateTemplateFieldRequired';
				var EnableNull = false;
				if (this.checked) { //设为必填
					EnableNull = true;
				} else { //设为可空
					EnableNull = false;
				}
				var param = { "ModelType": ModelType, "TemplateID": TemplateID, "FieldName": data.value, "EnableNull": EnableNull };
				UpdateSwitch(data, url, param);
			});
			//监听允许搜索开关
			form.on('switch(CheckOutSearch)', function (data) {
				var url = '/Admin/CommonModel/UpdateTemplateFieldSearch';
				var EnableShowOnSearchForm = false;
				if (this.checked) { //设为允许搜索
					EnableShowOnSearchForm = true;
				} else { //设为不允许搜索
					EnableShowOnSearchForm = false;
				}
				var param = { "ModelType": ModelType, "TemplateID": TemplateID, "FieldName": data.value, "EnableShowOnSearchForm": EnableShowOnSearchForm };
				UpdateSwitch(data, url, param);
			});
		});
	</script>
}